1
|
|
|
const fs = require('fs') |
2
|
|
|
const { join } = require('path') |
3
|
|
|
|
4
|
|
|
const outputHelper = require('../../helper/output') |
5
|
|
|
const TIMEOUT = 1000 |
6
|
|
|
|
7
|
|
|
describe('Output helper', () => { |
8
|
|
|
const spyError = jest.spyOn(console, 'error').mockImplementation(() => {}) |
|
|
|
|
9
|
|
|
const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {}) |
|
|
|
|
10
|
|
|
|
11
|
|
|
const file = 'src/test/output.txt' |
12
|
|
|
const path = join(file) |
13
|
|
|
|
14
|
|
|
it('output has empty data', () => { |
15
|
|
|
const result = outputHelper('', null) |
16
|
|
|
expect(result).toBe(null) |
17
|
|
|
}) |
18
|
|
|
|
19
|
|
|
it('output is console', () => { |
20
|
|
|
outputHelper(null, 'data') |
21
|
|
|
expect(spyLog).toHaveBeenCalled() |
22
|
|
|
expect(spyLog.mock.calls[0][0]).toContain('data') |
23
|
|
|
}) |
24
|
|
|
|
25
|
|
|
it('output to file', done => { |
26
|
|
|
fs.truncateSync(path) |
27
|
|
|
outputHelper(file, 'data') |
28
|
|
|
setTimeout(() => { |
29
|
|
|
try { |
30
|
|
|
expect(fs.readFileSync(path, 'utf8')).toBe('data\r\n') |
31
|
|
|
done() |
32
|
|
|
} catch (err) { |
33
|
|
|
done.fail(err) |
34
|
|
|
} |
35
|
|
|
}, TIMEOUT) |
36
|
|
|
}) |
37
|
|
|
|
38
|
|
|
it('output to not existed file', done => { |
39
|
|
|
const dummy = 'dummy.txt' |
40
|
|
|
outputHelper(dummy, 'data') |
41
|
|
|
setTimeout(() => { |
42
|
|
|
try { |
43
|
|
|
expect(spyError).toHaveBeenCalled() |
44
|
|
|
expect(spyError.mock.calls[0][0]).toContain('Cannot write file to') |
45
|
|
|
done() |
46
|
|
|
} catch (err) { |
47
|
|
|
done.fail(err) |
48
|
|
|
} |
49
|
|
|
}, TIMEOUT) |
50
|
|
|
}) |
51
|
|
|
|
52
|
|
|
it('output to write stream', done => { |
53
|
|
|
const writeable = fs.createWriteStream(path) |
54
|
|
|
outputHelper(writeable, 'data_stream') |
55
|
|
|
setTimeout(() => { |
56
|
|
|
try { |
57
|
|
|
expect(fs.readFileSync(path, 'utf8')).toBe('data_stream\r\n') |
58
|
|
|
done() |
59
|
|
|
} catch (err) { |
60
|
|
|
done.fail(err) |
61
|
|
|
} |
62
|
|
|
}, TIMEOUT) |
63
|
|
|
}) |
64
|
|
|
}) |
65
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.